package com.aauedu.auth.controller;

import com.aauedu.auth.service.SysRoleService;
import com.aauedu.auth.service.SysUserRoleService;
import com.aauedu.common.config.exception.MyException;
import com.aauedu.common.result.Result;
import com.aauedu.model.system.SysRole;
import com.aauedu.model.system.SysUserRole;
import com.aauedu.vo.system.AssginRoleVo;
import com.aauedu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javafx.scene.chart.ValueAxis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    //注入service
    @Resource
    private SysRoleService sysRoleService;


//    //查询所有角色
//    @GetMapping("/getAll")
//    public List<SysRole> getAll() {
//        //调用Service方法
//        List<SysRole> roleList = sysRoleService.list();
//        return roleList;
//    }

    //1 查询所有角色 和 当前用户所属的角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> map = sysRoleService.findRoleDataByUserId(userId);
//        List<SysRole> toAssignList = sysRoleService.findRoleDataByUserId(userId);
//        return Result.ok(toAssignList);
        return Result.ok(map);
    }

    //2 为用户分配角色

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assignRoleVo) {
        sysRoleService.doAssign(assignRoleVo);
        return Result.ok();
    }


    //统一返回结果
    @ApiOperation(value = "查询全部角色")
    @GetMapping("/getAll")
    public Result getAll() {
        //调用Service方法
//        try {
//            int i = 10 / 0;
//        } catch (Exception e) {
//            throw new MyException(20001, "执行了自定义异常处理...");
//        }
        return Result.ok(sysRoleService.list());
    }

    //条件分页查询
    //page 当前页 limit  每页显示的记录数  SysRoleQueryVo 条件对象
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo) {
        //调用service的方法实现
        //1 page 当前页 limit 每页显示的记录数
        Page<SysRole> pageParam = new Page<>(page, limit);
        //2 封装条件，判断条件是否为空，不为空则进行封装
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysRoleQueryVo.getRoleName())) {  //角色名称不为空
            //封装
            sysRoleLambdaQueryWrapper.like(SysRole::getRoleName, sysRoleQueryVo.getRoleName());
        }
        //3 调用方法实现
        IPage<SysRole> pageModel = sysRoleService.page(pageParam, sysRoleLambdaQueryWrapper);
        return Result.ok(pageModel);
    }

    //根据id获取角色信息
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据id查询")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    //新增角色
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")//get请求没有请求体，@RequestBody使用请求体，通过json格式传递数据
    public Result save(@RequestBody SysRole sysRole) {
        boolean flag = sysRoleService.save(sysRole);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail("添加角色信息失败");
        }
    }

    //修改角色
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysRole sysRole) {
        boolean flag = sysRoleService.updateById(sysRole);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail("更新角色信息失败");
        }
    }

    //根据id删除
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean flag = sysRoleService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail("删除角色信息失败！");
        }
    }

    //批量删除
    //前端数组[1,2,3]->后端List集合
    //前端json->后端对象
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean flag = sysRoleService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail("批量删除失败！");
        }
    }

}
